<template>
  <div id="inboundIndex">
    <el-row>
      <!-- 表格查询条件 -->
      <el-col :span="24">
        <el-form :inline="true" ref="stationForm" :model="queryForm">
          <el-form-item>
            <el-input v-model="queryForm.name" placeholder="入库单名称/编号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.userName" placeholder="入库人"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.warehouseName" placeholder="入库仓库"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.recordTypeCount" placeholder="入库类目数"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getTableDatas()">查询</el-button>
            <el-button @click="resetQueryForm" icon="el-icon-refresh-right">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <!-- 数据表格 -->
      <el-col :span="24">
        <el-row>
          <el-col :span="24">
            <el-button type="primary" icon="el-icon-plus" @click="toInbound">新增入库</el-button>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="moreDeletes">
              <el-button slot="reference" type="danger" icon="el-icon-delete">删除入库单</el-button>
            </el-popconfirm>
          </el-col>
        </el-row>
        <el-row style="margin-top:10px;">
          <el-col :span="24">
            <el-table :data="tableData" border style="width: 100%" @selection-change="tableDataSelected">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="code" label="编号"></el-table-column>
              <el-table-column prop="name" label="入库单名称"></el-table-column>
              <el-table-column prop="userName" label="入库人" width="75"></el-table-column>
              <el-table-column prop="warehouseName" label="入库仓库"></el-table-column>
              <el-table-column prop="inboundTime" label="入库时间"></el-table-column>
              <el-table-column prop="recordTypeCount" label="入库类目数" width="100"></el-table-column>
              <el-table-column prop="inCount" label="入库总量" width="100"></el-table-column>
              <el-table-column label="是否关联订单" width="110">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.orderID!=null" type="success">已关联</el-tag>
                  <el-tag v-else type="danger">未关联</el-tag>
                </template>
              </el-table-column>
              <el-table-column fixed="right" label="操作" align="center" width="110">
                <template slot-scope="scope">
                  <el-button @click="rowShow(scope.row)" size="small" icon="el-icon-search" circle title="查看"></el-button>
                  <!--                  <el-button @click="rowEdit(scope.row)" type="primary" size="small" icon="el-icon-edit" circle title="编辑"></el-button>-->
                  <el-button @click="rowDelete(scope.row)" type="danger" icon="el-icon-delete" size="small" circle title="删除"></el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="tableSizeChange"
              @current-change="tableCurrentChange"
              :current-page="pageData.page"
              :page-sizes="[10, 20, 40, 100]"
              :page-size="pageData.limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pageData.total"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-col>
    </el-row>



    <!-- 添加或修改-业务弹窗 -->
    <el-dialog
      title="入库单详情"
      :visible.sync="dialog.showDiv"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-descriptions class="margin-top" :column="3" border>
        <el-descriptions-item label="编号">{{ showData.inbound.code }}</el-descriptions-item>
        <el-descriptions-item label="名称">{{ showData.inbound.name }}</el-descriptions-item>
        <el-descriptions-item label="入库人">{{ showData.inbound.userName }}</el-descriptions-item>
        <el-descriptions-item label="入库时间">{{ showData.inbound.inboundTime }}</el-descriptions-item>
        <el-descriptions-item label="入库仓库">{{ showData.inbound.warehouseName }}</el-descriptions-item>
        <el-descriptions-item label="类目数量">{{ showData.inbound.recordTypeCount }}</el-descriptions-item>
        <el-descriptions-item label="入库总量">{{ showData.inbound.inCount }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ showData.inbound.comment }}</el-descriptions-item>
      </el-descriptions>
      <!-- 出库明细 -->
      <el-divider content-position="left">出库明细</el-divider>
      <el-table :data="showData.infoList" border style="width: 100%" >
        <el-table-column prop="recordName" label="入库物资"></el-table-column>
        <el-table-column prop="inCount" label="入库数量"></el-table-column>
      </el-table>
      <el-button
        @click="
							dialog.showDiv = false;
							resetAddOrEditOrShowForm();
						"
      >
        关闭
      </el-button>
    </el-dialog>


  </div>
</template>

<script>
import * as warehouseAPI from '@/api/wms/warehouse.js';
import * as staffAPI from '@/api/staff.js';
import * as recordAPI from '@/api/wms/record.js';
import * as axiosAPI from '@/api/wms/inbound.js';

export default {
  name: 'inboundIndex',
  data() {
    return {
      //查询表单数据
      queryForm: {
        name: null,
        userName: null,
        warehouseName: null,
        recordTypeCount: null
      },
      //表格数据
      tableData: [],
      //主表-分页数据
      pageData: {
        page: 1,
        size: 10,
        total: 0
      },
      //当前表格复选数据
      nowTableSelected: [],
      // 仓库下拉
      warehouseDataList:[],
      // 人员下拉
      staffDataList:[],
      // 物资下拉
      recordDataList:[],
      //弹窗状态
      dialog: {
        isEdit: false,
        saveOrEditDiv: false,
        showDiv: false
      },
      //添加编辑表单-检验
      saveOrEditDataFormRules:{
        userID: { required: true, message: '请输选择出库人', trigger: 'change' },
        warehouseID: { required: true, message: '请选择出库仓库', trigger: 'change' }
      },
      //展示表单数据
      showData: {
        inbound:{
          inboundID:null,
          comment:null,
          code:null,
          name:null,
          userID:null,
          userName:null,
          inboundTime:null,
          warehouseID:null,
          warehouseName:null,
          recordTypeCount:null,
          inCount:null,
          orderID:null
        },
        infoList:[]
      },
      //表单加载值
      formLoading: false
    };
  },
  mounted() {
    this.initWarehouseList();
    this.initStaffList();
    this.getTableDatas();
  },
  methods: {
    // 物资下拉列表初始化
    initRecordList(){
      let _this = this;
      recordAPI.list({"warehouseID":_this.saveOrEditData.outbound.warehouseID})
        .then(function(res){
          if(res.code == "0000"){
            _this.recordDataList = res.data;
          }
        })
    },
    // 仓库下拉列表初始化
    initWarehouseList(){
      let _this = this;
      warehouseAPI.list()
        .then(function(res){
          if(res.code == "0000"){
            _this.warehouseDataList = res.data;
          }
        })
    },
    // 人员下拉列表初始化
    initStaffList(){
      let _this = this;
      staffAPI.list({"name":null})
        .then(function(res){
          if(res.code == "0000"){
            _this.staffDataList = res.data;
          }
        })
    },
    //查询表格数据
    getTableDatas() {
      let _this = this;
      axiosAPI.pages(_this.queryForm, _this.pageData)
        .then(function(response) {
          console.log(response);
          if (response.code == '0000') {
            _this.tableData = response.data.rows;
            _this.pageData.total = response.data.total;
          }
        });
    },
    //重置查询条件表单
    resetQueryForm() {
      this.queryForm = {
        name: null,
        userName: null,
        warehouseName: null,
        recordTypeCount: null
      };
      this.getTableDatas();
    },
    //重置添加、编辑、展示表单
    resetAddOrEditOrShowForm() {
      this.showData = {
        inbound:{
          inboundID:null,
          comment:null,
          code:null,
          name:null,
          userID:null,
          userName:null,
          inboundTime:null,
          warehouseID:null,
          warehouseName:null,
          recordTypeCount:null,
          inCount:null,
          orderID:null
        },
        infoList:[]
      };
    },
    //提交-删除
    submitDelete(ids){
      let _this = this;
      axiosAPI.deletes(ids)
        .then(res => {
          if (res.code == '0000') {
            _this.getTableDatas();
          }
        });
    },
    //分页条目数切换
    tableSizeChange(size) {
      this.pageData.limit = size;
      this.getTableDatas();
    },
    //页码切换
    tableCurrentChange(page) {
      this.pageData.page = page;
      this.getTableDatas();
    },
    //行-查看
    rowShow(data) {
      var _this = this;
      axiosAPI.getByID(data.inboundID)
        .then(function(res){
          if(res.code == "0000"){
            _this.showData = res.data;
            _this.dialog.showDiv = true;
          }
      });
    },
    //行-编辑
    rowEdit(data) {
      this.dialog.saveOrEditDiv = true;
      this.saveOrEditData = JSON.parse(JSON.stringify(data));
      this.dialog.isEdit = true;
    },
    //行-删除
    rowDelete(data) {
      this.$confirm('确定要删除这行数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitDelete(data.inboundID);
      });
    },
    //点击批量删除
    moreDeletes() {
      if(this.nowTableSelected.length<=0){
        this.$notify.warning({
          title: '删除提示',
          message: '请至少选择一条数据再删除'
        });
        return;
      }
      let ids = '';

      for (var i in this.nowTableSelected) {
        ids += this.nowTableSelected[i].inboundID + ',';
      }

      ids = ids.substring(0, ids.length - 1);

      //调用删除
      this.submitDelete(ids);
    },
    //表格批量选择
    tableDataSelected(data){
      this.nowTableSelected = data;
    },
    // 出库人选择
    userSelectChange(data){
      if(data!=""){
        var resData = null;
        for(var i in this.staffDataList){
          if(this.staffDataList[i].staffID == data){
            resData = this.staffDataList[i];
            break;
          }
        }
        this.saveOrEditData.inbound.userID = resData.staffID;
        this.saveOrEditData.inbound.userName = resData.name;
      }else{
        this.saveOrEditData.inbound.userID = null;
        this.saveOrEditData.inbound.userName = null;
      }
    },
    // 出库仓库选择
    warehouseSelectChange(data){
      if(data!=""){
        var resData = null;
        for(var i in this.warehouseDataList){
          if(this.warehouseDataList[i].warehouseID == data){
            resData = this.warehouseDataList[i];
            break;
          }
        }
        this.saveOrEditData.inbound.warehouseID = resData.warehouseID;
        this.saveOrEditData.inbound.warehouseName = resData.name;

        this.initRecordList();

      }else{
        this.saveOrEditData.inbound.warehouseID = null;
        this.saveOrEditData.inbound.warehouseName = null;
      }

      this.saveOrEditData.infoList = [];
    }

  }
};
</script>

<style scoped></style>
